package com.xlf.common.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class User extends Model<User> {
	public static final User dao = new User();

	public Page<User> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from user order by u_id");
	}

	public Page<User> exeSearchSql(int pageNumber, int pageSize, String name, int roleType) {
		StringBuffer where = new StringBuffer(" from user where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if (name != null && !"".equals(name.trim())) {
			where.append(" and name like ?");
			param.add("%" + name + "%");
		}
		if(roleType > 0) {
			where.append(" and type=?");
			param.add(roleType);
		}

		where.append(" order by u_id");
		return paginate(pageNumber, pageSize, "select *", where.toString(), param.toArray());
	}

	public User exeFindByName(String username, String password) {
		return findFirst("select * from user where username=? and password=?", username, password);
	}

}
